using Senparc.Weixin.QY.AdvancedAPIs.Mass;
using Senparc.Weixin.QY.CommonAPIs;
using Senparc.Weixin.QY.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Senparc.Weixin.QY.AdvancedAPIs
{
	public static class MassApi
	{
		private const string URL_FORMAT = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={0}";
		public static MassResult SendText(string accessToken, string toUser, string toParty, string toTag, string agentId, string content, int safe = 0, int timeOut = 10000)
		{
			var data = new
			{
				touser = toUser,
				toparty = toParty,
				totag = toTag,
				msgtype = "text",
				agentid = agentId,
				text = new
				{
					content
				},
				safe = safe
			};
			return CommonJsonSend.Send<MassResult>(accessToken, "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={0}", data, CommonJsonSendType.POST, timeOut);
		}
		public static MassResult SendImage(string accessToken, string toUser, string toParty, string toTag, string agentId, string mediaId, int safe = 0, int timeOut = 10000)
		{
			var data = new
			{
				touser = toUser,
				toparty = toParty,
				totag = toTag,
				msgtype = "image",
				agentid = agentId,
				image = new
				{
					media_id = mediaId
				},
				safe = safe
			};
			return CommonJsonSend.Send<MassResult>(accessToken, "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={0}", data, CommonJsonSendType.POST, timeOut);
		}
		public static MassResult SendVoice(string accessToken, string toUser, string toParty, string toTag, string agentId, string mediaId, int safe = 0, int timeOut = 10000)
		{
			var data = new
			{
				touser = toUser,
				toparty = toParty,
				totag = toTag,
				msgtype = "voice",
				agentid = agentId,
				voice = new
				{
					media_id = mediaId
				},
				safe = safe
			};
			return CommonJsonSend.Send<MassResult>(accessToken, "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={0}", data, CommonJsonSendType.POST, timeOut);
		}
		public static MassResult SendVideo(string accessToken, string toUser, string toParty, string toTag, string agentId, string mediaId, string title, string description, int safe = 0, int timeOut = 10000)
		{
			var data = new
			{
				touser = toUser,
				toparty = toParty,
				totag = toTag,
				msgtype = "video",
				agentid = agentId,
				video = new
				{
					media_id = mediaId,
					title = title,
					description = description
				},
				safe = safe
			};
			return CommonJsonSend.Send<MassResult>(accessToken, "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={0}", data, CommonJsonSendType.POST, timeOut);
		}
		public static MassResult SendFile(string accessToken, string toUser, string toParty, string toTag, string agentId, string mediaId, int safe = 0, int timeOut = 10000)
		{
			var data = new
			{
				touser = toUser,
				toparty = toParty,
				totag = toTag,
				msgtype = "file",
				agentid = agentId,
				file = new
				{
					media_id = mediaId
				},
				safe = safe
			};
			return CommonJsonSend.Send<MassResult>(accessToken, "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={0}", data, CommonJsonSendType.POST, timeOut);
		}
		public static MassResult SendNews(string accessToken, string toUser, string toParty, string toTag, string agentId, List<Article> articles, int safe = 0, int timeOut = 10000)
		{
			var data = new
			{
				touser = toUser,
				toparty = toParty,
				totag = toTag,
				msgtype = "news",
				agentid = agentId,
				news = new
				{
					articles = (
						from z in articles
						select new
						{
							title = z.Title,
							description = z.Description,
							url = z.Url,
							picurl = z.PicUrl
						}).ToList()
				}
			};
			return CommonJsonSend.Send<MassResult>(accessToken, "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={0}", data, CommonJsonSendType.POST, timeOut);
		}
		public static MassResult SendMpNews(string accessToken, string toUser, string toParty, string toTag, string agentId, List<MpNewsArticle> articles, int safe = 0, int timeOut = 10000)
		{
			var data = new
			{
				touser = toUser,
				toparty = toParty,
				totag = toTag,
				msgtype = "mpnews",
				agentid = agentId,
				mpnews = new
				{
					articles = (
						from z in articles
						select new
						{
							z.title,
							z.thumb_media_id,
							z.author,
							z.content_source_url,
							z.content,
							z.digest,
							z.show_cover_pic
						}).ToList()
				},
				safe = safe
			};
			return CommonJsonSend.Send<MassResult>(accessToken, "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={0}", data, CommonJsonSendType.POST, timeOut);
		}
	}
}
